package com.adhere.utils;

import com.adhere.entity.RespResult;
import org.apache.commons.lang3.StringUtils;

/**
 * 封装返回结果的工具类
 */
public class RespResultUtils {

    public static RespResult getSuccess(String message) {
        return getSuccess(null, message);
    }

    public static <T> RespResult getSuccess(T data, String message) {
        RespResult respResult = new RespResult<>();
        respResult.setCode(200);
        respResult.setSuccess(true);
        respResult.setData(data);
        respResult.setTimestamp(String.valueOf(System.currentTimeMillis()));
        if (StringUtils.isNotEmpty(message)) {
            respResult.setMessage(message);
        } else {
            respResult.setMessage("success");
        }
        return respResult;
    }

    public static RespResult getFail(String message) {
        return getFail(500, message);
    }

    public static RespResult getFail(Integer code, String message) {
        RespResult respResult = new RespResult<>();
        respResult.setCode(code);
        respResult.setSuccess(false);
        respResult.setData(null);
        respResult.setTimestamp(String.valueOf(System.currentTimeMillis()));
        if (StringUtils.isNotEmpty(message)) {
            respResult.setMessage(message);
        } else {
            respResult.setMessage("fail");
        }
        return respResult;
    }
}
